/* 设置body的fontSize */
var setClientfontSize = function () {
  var winClient = document.documentElement.clientWidth;
  var fontSize = winClient < 640 ? winClient / 16 : 20;
  if (fontSize < 20) {
    fontSize = 20;
  }
  document.getElementsByTagName("html")[0].style.fontSize = fontSize + "px";
};
setClientfontSize();
window.onresize = function () {
  setClientfontSize();
};

// 创建音频对象
var chipsmoving = new Audio('./sound/chipsmoving.mp3');
var click = new Audio('./sound/click.mp3');
var ding = new Audio('./sound/ding.mp3');
var lose = new Audio('./sound/lose.mp3');
var reordercard = new Audio('./sound/reordercard.mp3');
var sendcardg = new Audio('./sound/sendcard.mp3');
var win = new Audio('./sound/win.mp3');

// gif保存在内存
window.state_border_red_10s_blob = ""
window.state_border_red_10s_url = ""
fetch('./images/state_border_red_10s.gif')
  .then(r => r.blob())
  .then(b => window.state_border_red_10s_blob = b)
  .then(u => {
    //console.log(u)
  });

// 创建52张牌
var pokerCardsTypes = ["S", "H", "C", "D"]; // 黑桃、红心、梅花、方块
var pokerCardsNumbers = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

// 设置状态（参数包括，需要设置状态的元素，具体状态，筹码）
function setState(target, state, chips) {
  // 开个声音
  state === 'status__info--countdown' ? true : click.play();

  // 需要设置状态的元素
  var targetElement = document.getElementsByClassName(target)[0];

  // 设置status__info--countdown
  if (state === 'status__info--countdown') {
    window.state_border_red_10s_url = URL.createObjectURL(window.state_border_red_10s_blob)
    targetElement.getElementsByClassName('status__info--big')[0].style.backgroundImage = 'url(' + window.state_border_red_10s_url + ')'
    // targetElement.getElementsByClassName('status__info--big')[0].style.backgroundImage = 'url(./images/state_border_red_10s.gif?rand=' + Math.random() + ')'
  }
  else {
    URL.revokeObjectURL(window.state_border_red_10s_url)
    targetElement.getElementsByClassName('status__info--big')[0].style.backgroundImage = ''
  }

  // 需要设置状态的元素
  var targetElement = document.getElementsByClassName(target)[0];

  // 状态
  targetElement.className = 'status ' + target + ' ' + state;

  // 抛出的筹码
  if (chips === undefined) return;
  var chipsInvested = targetElement.getElementsByClassName('status__number')[0];
  chipsInvested.innerHTML = chips;
  // 剩下的筹码
  targetElement.getElementsByClassName('status__chips--Leftover')[0].innerHTML = Number(targetElement.getElementsByClassName('status__chips--Leftover')[0].innerHTML) - chips

  // 底池
  document.getElementsByClassName('center__pot--text')[0].innerHTML = Number(document.getElementsByClassName('center__pot--text')[0].innerHTML) + chips

};

// 点击加注时调用
function raise() {
  click.play();
  document.getElementsByClassName('my__items--raise')[0].style.display === 'block' ?
    document.getElementsByClassName('my__items--raise')[0].style.display = 'none' :
    document.getElementsByClassName('my__items--raise')[0].style.display = 'block';
}



// 开始前渲染
function renderBeforeStarting() {
  document.getElementsByClassName('center__pot--text')[0].innerHTML = '当前底池' + pot;
  document.getElementsByClassName('center__item--room-number')[0].innerHTML = '房间号：' + roomNumber;
  document.getElementsByClassName('center__item--ante')[0].innerHTML = '大盲：' + ante;
  for (var i = 0; i < playersMsg.length; i++) {
    var target = document.getElementsByClassName('status--' + (i + 1))[0];
    playersMsg[i].id !== null ? target.id = playersMsg[i].id : false;
    target.getElementsByClassName('status__name')[0].innerHTML = playersMsg[i].name;
    target.getElementsByClassName('status__avatar')[0].src = playersMsg[i].avatar || './images/null.png';
    target.getElementsByClassName('status__chips--Leftover')[0].innerHTML = playersMsg[i].statusChipsLeftover;
  }
}

// 每局开始时渲染
function renderEveryRound() {
  for (var i = 0; i < playersMsg.length; i++) {
    var target = document.getElementsByClassName('status--' + (i + 1))[0];
    playersMsg[i].isDealer === true ?
      target.getElementsByClassName('status__dealer')[0].className = "status__dealer status__dealer--hook" :
      false;
    //不是dealer取消这个

    if (playersMsg[i].isTrun) {
      target.className =
        'status ' + 'status--' + (i + 1) + ' ' + 'status__info--countdown';
      // 这个可以不摧毁，点击哪里会摧毁这个
      window.state_border_red_10s_url = URL.createObjectURL(window.state_border_red_10s_blob)
      target.getElementsByClassName('status__info--big')[0].style.backgroundImage = 'url(' + window.state_border_red_10s_url + ')'
    }
  }
}

// 渲染底牌
function renderTheFlop() {
  document.getElementsByClassName('my__img--the-flop-1')[0].src = './images/pokerCards/' + theFlop[0] + '.png';
  document.getElementsByClassName('my__img--the-flop-2')[0].src = './images/pokerCards/' + theFlop[1] + '.png';
}

// 渲染公共牌
var renderCommunityCardPokerTarget = document.getElementsByClassName('center__img')
function renderCommunityCardPoker() {
  renderCommunityCardPokerTarget[0].src = './images/pokerCards/' + communityCardPoker[0] + '.png';
  renderCommunityCardPokerTarget[1].src = './images/pokerCards/' + communityCardPoker[1] + '.png'
  renderCommunityCardPokerTarget[2].src = './images/pokerCards/' + communityCardPoker[2] + '.png'
  communityCardPoker[3] ? renderCommunityCardPokerTarget[3].src = './images/pokerCards/' + communityCardPoker[3] + '.png' : false;
  communityCardPoker[4] ? renderCommunityCardPokerTarget[4].src = './images/pokerCards/' + communityCardPoker[4] + '.png' : false;
}
function renderCommunityCardPokerbegin() { //这个函数把桌面清空
  renderCommunityCardPokerTarget[0].src = false;
  renderCommunityCardPokerTarget[1].src = false;
  renderCommunityCardPokerTarget[2].src = false;
  renderCommunityCardPokerTarget[3].src = false;
  renderCommunityCardPokerTarget[4].src = false;
}

// 设置位置，保证玩家在下面


function setLocation() {
  // playersMsg.splice(
  //   playerNumber,
  //   0, // 以6为下标，替换
  //   ...playersMsg.splice(
  //     playersMsg[0+1],
  //     playerNumber, // 以0为下标，删除0-6, 返回0-6
  //   )
  // )
  var MapPlayersMsg = []
  for (var i = 0; i < playersMsg.length - playerNumber; i++) {
    MapPlayersMsg.push(playersMsg[playerNumber + i])
  }
  for (var i = 0; i < playerNumber; i++) {
    MapPlayersMsg.push(playersMsg[i])
  }
  window.playersMsg = MapPlayersMsg;
}


// 设置观战模式
function setSpectatorMode() {
  document.getElementsByClassName('my__buttons')[0].style.display = 'none'
}





